package com.mp.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mp.common.Result;
import com.mp.goods.bean.StdValue;
import com.mp.goods.dto.InsertStdValueDTO;
import com.mp.goods.service.StdValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 规格值表 前端控制器
 * </p>
 *
 * @author notejava
 * @since 2020-07-11
 */
@RestController
@RequestMapping("/goods/std-value")
@Api(tags = "商品规格值api")
public class StdValueController {

    @Resource
    private StdValueService stdValueService;


    @PostMapping(value="/insert")
    @ApiOperation(value="新增规格值")
    public Result insert(@RequestBody List<InsertStdValueDTO> model){
        for (InsertStdValueDTO insertStdValueDTO:model) {
            insertStdValueDTO.setId(null);
            Long standardId = insertStdValueDTO.getStandardId();
            QueryWrapper<StdValue> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(StdValue::getStandardId,standardId);
            stdValueService.remove(wrapper);
        }
        Boolean insert = stdValueService.insert(model);
        if(insert){
            return Result.success(model);
        }else{
            return Result.fail(null);
        }
    }

    @GetMapping(value="/selectList/{standardId}")
    @ApiOperation(value="查询规格值")
    public Result selectList(@PathVariable Long standardId){
        QueryWrapper<StdValue> wrapper = new QueryWrapper();
        wrapper.lambda().eq(StdValue::getStandardId,standardId);
        return Result.success(stdValueService.list(wrapper));
    }

}

